package cn.schoolwow.workflow.module.instance.action.flow.start;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.module.common.domain.QuickWorkFlowOption;

import java.util.List;

public class ExecuteAfterInstanceFinishListenerFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        QuickWorkFlowOption quickWorkFlowOption = flowContext.checkInstanceData(QuickWorkFlowOption.class);

        if(null==quickWorkFlowOption.workFlowInstanceListener){
            return;
        }

        {
            WorkFlowInstance workFlowInstance = flowContext.getInstanceData(WorkFlowInstance.class);
            if(null!=workFlowInstance){
                quickWorkFlowOption.workFlowInstanceListener.afterInstanceFinish(workFlowInstance);
            }
        }
        {
            List<WorkFlowInstance> workFlowInstanceList = flowContext.getData("workFlowInstanceList",List.class);
            if(null!=workFlowInstanceList&&!workFlowInstanceList.isEmpty()){
                for(WorkFlowInstance workFlowInstance:workFlowInstanceList){
                    quickWorkFlowOption.workFlowInstanceListener.afterInstanceFinish(workFlowInstance);
                }
            }
        }
    }

    @Override
    public String name() {
        return "工作流实例结束之后";
    }
}
